package com.globallogic.socialnetwork.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;

import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;

import com.globallogic.socialnetwork.dao.UserDao;
import com.globallogic.socialnetwork.entity.Group;
import com.globallogic.socialnetwork.entity.Message;
import com.globallogic.socialnetwork.entity.User;
import com.globallogic.socialnetwork.util.DaoException;

/**
 * Implementation of the Service Layer.
 */
@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class UserService {

        @Autowired
        private UserDao userDao;

        private final static Logger log = Logger
                        .getLogger("SocialNetworkFileLogger");

        public UserService() {
        }

        /**
         * Registers new user.
         * 
         * @param user
         *            the user to register
         * @throws DaoException
         *             if user with the same name already exists
         */

        public boolean register(User user) throws DaoException {
                if (userDao.getUserByLogin(user.getLogin()) != null) {
                        return false;
                } else {
                        userDao.createUser(user);
                        return true;
                }
        }

        /**
         * Login
         * 
         * @param login
         * @param password
         * @return User, if user with such login, password exist, otherwise null
         * @throws DaoException
         */

        public User login(String login, String password) throws DaoException {
                User user = userDao.getUserByLoginPassword(login, password);
                return user;

        }

        /**
         * Return user by login
         * 
         * @param login
         * @return found <tt>User</tt>, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public User getUser(String login) throws DaoException {
                User user = userDao.getUserByLogin(login);
                return user;
        }

        /**
         * Update user
         * 
         * @param user
         * @throws DaoException
         */
        public void updateUser(User user) throws DaoException {
                userDao.updateUser(user);
        }

        /**
         * Get all users
         * 
         * @return list of <tt>User</tt>, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public List<User> getAllUsers() throws DaoException {
                return userDao.getAllUsers();
        }

        /**
         * Get all user's friend
         * 
         * @param login
         * @return list of friends, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public List<User> getFriends(String login) throws DaoException {
                User user = userDao.getUserByLogin(login);
                List<User> friends = userDao.getFriends(user);
                if (friends.size() == 0) {
                        return new ArrayList<User>();
                }
                return friends;
        }

        /**
         * Get all user's messages
         * 
         * @param login
         * @return list of <tt>messages</tt>, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public List<Message> getAllMessages(String login) throws DaoException {
                User user = userDao.getUserByLogin(login);
                List<Message> messages = userDao.getAllMessages(user);
                if (messages.size() == 0) {
                        return new ArrayList<Message>();
                }
                return messages;
        }

        /**
         * Get all user's invitations
         * 
         * @param login
         * @return list of new friends, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public List<User> getNewFriends(String login) throws DaoException {
                User user = userDao.getUserByLogin(login);
                List<User> newFriends = userDao.getNewFriends(user);
                if (newFriends.size() == 0) {
                        return null;
                }
                return newFriends;
        }

        /**
         * Get list of users by filter
         * 
         * @param filter
         *            attribute's name in class {@link User} for search users
         * @param condition
         *            condition for search users
         * @return list of <tt>User</tt>, <tt>null</tt> if not found.
         * @throws DaoException
         */
        public List<User> getUsersByFilter(String filter, String condition)
                        throws DaoException {
                return userDao.getUsersByFilter(filter, condition);
        }

        /**
         * Follow one user to another
         * 
         * @param proposer
         * @param accepter
         * @throws DaoException
         */
        public void followUser(String proposer, User accepter) throws DaoException {
                User user = userDao.getUserByLogin(proposer);
                userDao.followUser(user, accepter);
        }

        /**
         * Accept invitation
         * 
         * @param user
         *            User, that accept invitation
         * @param proposer
         *            User, that send invitation
         * @throws DaoException
         */
        public void acceptFriend(String login, User accepter) throws DaoException {
                User user = userDao.getUserByLogin(login);
                userDao.acceptFriend(user, accepter);
        }

        /**
         * Send message
         * 
         * @param senderUser
         *            User, that send message
         * @param receiverUser
         *            User, that receive message
         * @param message
         *            text of message
         * @throws DaoException
         */
        public void sendMessage(String sender, String receiver, String message)
                        throws DaoException {
                log.warn("***UserService.sendmessage starts");
                User senderUser = getUser(sender);
                User receiverUser = getUser(receiver);
                userDao.sendMessage(senderUser, receiverUser, message);

        }

        /**
         * Mark message as read
         * 
         * @param selectedMessage
         * @throws DaoException
         */
        public void markAsRead(Message selectedMessage) throws DaoException {
                userDao.markAsRead(selectedMessage);

        }

        /**
         * Remove message
         * 
         * @param selectedMessage
         * @throws DaoException
         */
        public void removeMessage(Message selectedMessage) throws DaoException {
                userDao.removeMessage(selectedMessage);

        }

        /**
         * Remove friend
         * 
         * @param selectedFriend
         * @param currentUser
         * @throws DaoException
         */
        public void removeFriend(User selectedFriend, User currentUser)
                        throws DaoException {
                userDao.removeFriend(selectedFriend, currentUser);

        }

        /**
         * Get groups from db
         * 
         * @param user
         *            current user
         * @param condition
         *            condition for search groups
         * @return found list of groups, if condition="" - list of all groups
         * @throws DaoException
         */
        @SuppressWarnings("unchecked")
        public List<Group> searchGroups(User user, String condition)
                        throws DaoException {
                List<Group> founded = userDao.searchGroups(condition);
                List<Group> userGroups = user.getGroups();
                List<Group> temp = new ArrayList<Group>();
                for (Group group : founded) {
                        for (Group uGroup : userGroups) {
                                if (group.getIdGroup() == uGroup.getIdGroup())
                                        temp.add(group);
                        }
                }
                founded = ListUtils.removeAll(founded, temp);

                return founded;
        }

        public List<Group> getUserGroups(User user) throws DaoException {
                return userDao.searchUserGroups(user);
        }

        public void setUserDao(UserDao userDao) {
                this.userDao = userDao;
        }
}